global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_A2_industry_list.log, replace name(tabl)

capture noi {
    
    * Table_industry_list

    texdoc init ${tab_dir}/appendix/Table_A2_industry_list.tex, replace
    texdoc stlog, nolog

    * run baseline regression with F+IY FE to get firms and industries
    use ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear
    estimates clear
    clonevar LSW = lswMPm_1995_a
    clonevar HSW = hswMPm_1995_a
    clonevar VAEMP = vaempMPm_1995_a
    clonevar GDPGAP = lngdpgap_1995_a
    clonevar GDPPC = gdppcMPm_1995_a
    clonevar spilloversown = spillauto95bia_1995_a
    clonevar spilloversother = spillNauto95bia_1995_a
    clonevar spilloversownzero = spillauto95bia_1995_a0
    clonevar spilloversotherzero = spillNauto95bia_1995_a0
    clonevar stockown = k${depvar}_${ttt} 
    clonevar stockownzero = k${depvar}_${ttt}0  
    clonevar stockother = kNOT_${depvar}_${ttt} 
    clonevar stockotherzero = kNOT_${depvar}_${ttt}0

    bys lse_id : egen _total_auto95_bia_1995 = sum(auto95_bia) if year>=1995+2 & year <= 2009+2
    bys lse_id : egen total_auto95_bia_1995 = max(_total_auto95_bia_1995)
    drop _total_auto95_bia_1995
    egen yearindustry = group(year industry) if year <= 2009
    sort lse_id year
    *run baseline (table 5) regression to restrict to sample
    gen F2_auto95_bia = F2.auto95_bia
    ppmlhdfe F2_auto95_bia LSW HSW GDPGAP stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_auto95_bia_1995>0, absorb(lse_id yearindustry) vce(cluster lse_id)

    keep if e(sample)
    tempfile a1
    save `a1', replace

    *get automation patents of regression firms 
    use `a1', clear
    collapse (sum)F2_auto95_bia, by(industry)
    tempfile f1
    save `f1', replace

    *count firms by industry
    use `a1', clear
    keep BvD industry
    duplicates drop
    gen byte firms=1
    collapse (sum)firms, by(industry)
    tempfile f2
    save `f2', replace

    * output summary stats on industries
    import delimited using ${final_dir}/industries.csv, varnames(1) clear
    tostring code, gen(industry) format(%02.0f)
    drop code
    mmerge industry using `f1', unmatched(using)
    mmerge industry using `f2', unmatched(using)
    replace industry = "NA" if industry=="Other"

    *generate shares and adjust descriptions for NA
    egen totpats = sum(F2)
    egen totfirms = sum(firms)
    gen share_patents = round(100*F2 / totpats,0.01)
    gen share_firms = round(100*firms / totfirms,0.01)
    keep industry description share*
    replace industry = "A" if share_patents < 1.0
    replace description = "Other industries" if share_patents < 1.0
    collapse (sum)share* (first)description, by(industry)
    sort industry
    replace industry = "" if industry == "A"
    replace description = "No information on industry" if industry == "NA"
    replace industry = "" if industry == "NA"

    *save to texdoc
    forval i = 1/`=_N' { 
        local share_patents`i' : di %10.2f share_patents[`i'] 
        local share_firms`i' : di %10.2f share_firms[`i']
        sleep 300
        texdoc local row_`i' = industry[`i'] + " & " + description[`i'] + " & " + "`share_patents`i''" + " & " + "`share_firms`i''" + " \\"
    }

    * output table (via texdoc)
    texdoc local notes "The table reports the industry of patenting ﬁrms included in our baseline regression with industry-year fixed effects at the NACEv2 division level, and the share of biadic auto95 families for each industry. Industries representing less than 1\% of patents are summed up in the 'Other industries' category."
    texdoc stlog close
    qui include ${code_dir}/config/texdoctools.do
    tex `texpreamble'

    /*tex
    \begin{tabular}{@{}ll*{2}{S[table-format = 2.2]}@{}}
    \toprule
    & Industry & \mc{Share auto95 (\%)} & \mc{Share firms (\%)} \\
    \midrule
    `row_1'
    `row_2'
    `row_3'
    `row_4'
    `row_5'
    `row_6'
    `row_7'
    `row_8'
    `row_9'
    `row_10'
    `row_11'
    `row_12'
    \bottomrule
    \end{tabular}
    tex*/
    tex `texpostamble'
    texdoc close

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close tabl